package com.dp.service.impl3;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.JsonPage;
import com.dp.service.impl3.BaseImpl;
import com.dp.util.KeyGenerator;
import com.dp.vo.TGenre;

@Service
@Transactional
public class GenreServiceImpl3 extends BaseImpl { // 以前是实现com.dp.service.GenreService接口

	/**
	 * 查询
	 * 
	 * @return
	 */
	public Long findGenreCount(TGenre genre,JsonPage jpage) {
			StringBuffer hql = new StringBuffer("SELECT count(a) FROM TGenre a where 1=1 ");
			if (genre != null) {
				if (genre.getGenreName() != null
						&& !"".equals(genre.getGenreName())) {
					hql.append(" and a.genreName like '%" + genre.getGenreName() + "%'");
				}
				if (genre.getConentType() != null
						&& !"".equals(genre.getConentType())) {
					hql.append(" and a.conentType = " + genre.getConentType());
				}
			}
			Object obj = getSingleResult(hql.toString());
			return obj == null ? 0L : (Long) obj;
	}

	/**
	 * 查询,分页
	 * 
	 * @return
	 */
	public List<TGenre> findGenre(TGenre genre,JsonPage jpage, int firstrow, int maxrow) {
		StringBuffer hql = new StringBuffer("SELECT a FROM TGenre a where 1=1 ");
		if (genre != null) {
			if (genre.getGenreName() != null
					&& !"".equals(genre.getGenreName())) {
				hql.append(" and a.genreName like '%" + genre.getGenreName() + "%'");
			}
			if (genre.getConentType() != null
					&& !"".equals(genre.getConentType())) {
				hql.append(" and a.conentType = " + genre.getConentType());
			}
		}
		if(jpage!=null){
			if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
				hql.append(" order by a."+jpage.getSidx());
				hql.append(" "+jpage.getSord());
			}
		}
		return getQueryList(hql.toString(), firstrow, maxrow);
	}

	/*public Genre findAdById(String genreid) {
		String hql = "SELECT a FROM TGenre a where 1=1 ";
		if (genreid != null && !"".equals(genreid)) {
			hql += " and a.genreId = '" + genreid + "'";
		}
		return (Genre)getSingleResult(hql);
	}*/

	public int update(TGenre genre) {
		String hql = "UPDATE TGenre SET genreName='" + genre.getGenreName()
				+ "',conentType=" + genre.getConentType()
				+ " where genreId='" + genre.getGenreId()+"'";
		return super.updateHql(hql);
	}

	public Object save(TGenre genre) {
		String genreId = com.dp.util.BaseBean.ObjectToString(KeyGenerator.getInstance().getNextKeyValue("t_genre"));	//获取主键（非自增）
		genre.setGenreId(genreId);
		return super.save(genre);
	}
/*没用
	public int checkGenreName(String genrename, String old_genrename) {
		String hql = "SELECT a FROM TGenre a where 1=1 ";
		if (genrename != null && !"".equals(genrename)) {
			hql += " and a.genreName = '" + genrename + "'";
		}
		if (old_genrename != null && !"".equals(old_genrename)) {
			hql += " and a.genreName != '" + old_genrename + "'";
		}
		return getSingleResult(hql)==null ? 0 : 1;
	}
	*/

	/**
	 * 检查是否已存在：类别名称和内容类型都相同时，已存在。
	 * @param genrename
	 * @return
	 */
	public TGenre checkGenre(TGenre genre) {
		String hql = "SELECT a FROM TGenre a where 1=1 ";
		if (genre.getGenreName() != null && !"".equals(genre.getGenreName())) {
			hql += " and a.genreName = '" + genre.getGenreName() + "'";
		}
		if (genre.getConentType() != null && !"".equals(genre.getConentType())) {
			hql += " and a.conentType = '" + genre.getConentType() + "'";
		}
		return (TGenre)getSingleResult(hql);
	}

	/**
	 * Delete
	 * 
	 * @return boolean b
	 */
	public boolean deleteGenreById(String genreid) {
		String jpql = "DELETE TGenre n WHERE n.genreId='" + genreid + "'";
		if (super.updateHql(jpql) > 0)
			return true;
		else
			return false;
	}

}
